//
//  ActivityParticularsViewController.m
//  Fengly_Nuo_Movie
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 Fengly_Nuo. All rights reserved.
//

#import "ActivityParticularsViewController.h"
#import "ActivityModel.h"
#import "UIImageView+WebCache.h"
#import "Fengly_nuo_SQLDataBaseHandle.h"

// MARK:VIEW_CENTER_X:屏中心x轴
#define VIEW_CENTER_X self.view.center.x

// MARK:VIEW_CENTER_Y:屏中心y轴
#define VIEW_CENTER_Y self.view.center.y

// MARK:HEIGHT:屏高
#define HEIGHT self.view.frame.size.height

// MARK:WIDTH:屏宽
#define WIDTH self.view.frame.size.width

#define kImageViewWidth imageView.frame.size.width
#define kImageView_y imageView.frame.origin.y

#define kDateLabelWidth (WIDTH - kImageViewWidth - 80)
#define kDateLabel_x (kImageViewWidth + 60)

#define kDateImageHeight dateImage.frame.size.height
#define kDateImageWidth dateImage.frame.size.width
#define kDateImage_x dateImage.frame.origin.x

@interface ActivityParticularsViewController ()

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, assign) CGFloat bigLabelHelght;

@end

@implementation ActivityParticularsViewController

- (void)dealloc {
    Block_release(_block);
    [_model release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // 计算文本高度
    [self slefHeight];
    // 主页面设置
    [self createAPView];
    // 页面铺设
    [self createAllInfoView];
}


// 计算文本高度
- (void)slefHeight {
    
    // 求label 高度
    // 文字的大小，描述文字大小的信息
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    // 通过文字大小 获取文本的高度
    
    CGRect rect = [_model.content boundingRectWithSize:CGSizeMake(WIDTH - 40, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    // 属性传值 将label高度给 bigLabelHelght
    _bigLabelHelght = rect.size.height;

}


// 主页面设置
- (void)createAPView {

    self.navigationItem.title = _model.title;
    // right bar button item
    UIImage *imageCollect = [UIImage imageNamed:@"btn_nav_share.png"];
    imageCollect = [imageCollect imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    UIBarButtonItem *collectButtonItem = [[UIBarButtonItem alloc] initWithImage:imageCollect style:(UIBarButtonItemStylePlain) target:self action:@selector(collectButtonItemAction:)];
    self.navigationItem.rightBarButtonItem = collectButtonItem;
   
    // left bar button item
    UIImage *imageBack = [UIImage imageNamed:@"btn_nav_back.png"];
    imageBack = [imageBack imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithImage:imageBack style:(UIBarButtonItemStylePlain) target:self action:@selector(backButtonItemAction:)];
    self.navigationItem.leftBarButtonItem = backButtonItem;
    
    // creat scrollView
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:_scrollView];
    [_scrollView release];
    // offset
    _scrollView.contentSize = CGSizeMake(0, _bigLabelHelght + HEIGHT * 0.25 + 250);
    // cancel springback
//    _scrollView.bounces = NO;
    // cancel Scroll Indicator
    _scrollView.showsVerticalScrollIndicator = NO;
}


// 页面铺设
- (void)createAllInfoView {
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, WIDTH - 40, 50)];
    titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [_scrollView addSubview:titleLabel];
    [titleLabel release];
    titleLabel.text = _model.title;
    
    // bigImage
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, titleLabel.frame.size.height + 10 + 10, WIDTH * 0.3, HEIGHT * 0.25)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:_model.image]];
    [_scrollView addSubview:imageView];
    [imageView release];
    
    // dateImage and dateLabel
    UIImageView *dateImage = [[UIImageView alloc] initWithFrame:CGRectMake(kImageViewWidth + 30, kImageView_y + 5, 25, 25)];
    dateImage.image = [UIImage imageNamed:@"icon_date_blue.png"];
    [_scrollView addSubview:dateImage];
    [dateImage release];
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(kDateLabel_x, kImageView_y + 5, kDateLabelWidth, kDateImageHeight)];
    NSString *begin_Str = [_model.begin_time substringWithRange:NSMakeRange(5, 11)];
    NSString *end_Str = [_model.end_time substringWithRange:NSMakeRange(5, 11)];
    NSString *begin_End_str = [NSString stringWithFormat:@"%@ -- %@", begin_Str, end_Str];
    dateLabel.text = begin_End_str;
    dateLabel.font = [UIFont systemFontOfSize:15];
    [_scrollView addSubview:dateLabel];
    [dateLabel release];
    
    // sponsorImage and sponsorImage
    UIImageView *sponsorImage = [[UIImageView alloc] initWithFrame:CGRectMake(kImageViewWidth + 30, kImageView_y + 35, kDateImageWidth, kDateImageHeight)];
    sponsorImage.image = [UIImage imageNamed:@"icon_sponsor_blue.png"];
    [_scrollView addSubview:sponsorImage];
    [sponsorImage release];
    UILabel *sponsorLabel = [[UILabel alloc] initWithFrame:CGRectMake(kDateLabel_x, kImageView_y + 35, kDateLabelWidth, kDateImageHeight)];
    sponsorLabel.text = _model.ownerName;
    sponsorLabel.font = [UIFont systemFontOfSize:15];
    [_scrollView addSubview:sponsorLabel];
    [sponsorLabel release];
    
    // catalogImage and catalogLabel
    UIImageView *catalogImage = [[UIImageView alloc] initWithFrame:CGRectMake(kImageViewWidth + 30, kImageView_y + 70, kDateImageWidth, kDateImageHeight)];
    catalogImage.image = [UIImage imageNamed:@"icon_catalog_blue.png"];
    [_scrollView addSubview:catalogImage];
    [catalogImage release];
    UILabel *catalogLabel = [[UILabel alloc] initWithFrame:CGRectMake(kDateLabel_x, kImageView_y + 70, kDateLabelWidth, kDateImageHeight)];
    catalogLabel.text = _model.category_name;
    catalogLabel.font = [UIFont systemFontOfSize:15];
    [_scrollView addSubview:catalogLabel];
    [catalogLabel release];
    
    UIImageView *spotImage = [[UIImageView alloc] initWithFrame:CGRectMake(kImageViewWidth + 30, kImageView_y + 105, kDateImageWidth, kDateImageHeight)];
    spotImage.image = [UIImage imageNamed:@"icon_spot_blue.png"];
    [_scrollView addSubview:spotImage];
    [spotImage release];
    UILabel *spotLabel = [[UILabel alloc] initWithFrame:CGRectMake(kDateLabel_x, kImageView_y + 105, kDateLabelWidth, kDateImageHeight * 2)];
    spotLabel.text = _model.address;
    spotLabel.font = [UIFont systemFontOfSize:15];
    spotLabel.numberOfLines = 2;
    [_scrollView addSubview:spotLabel];
    [spotLabel release];
    
    UILabel *activityIntroduceLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, HEIGHT * 0.25 + 80, WIDTH - 40, 50)];
    activityIntroduceLabel.text = @"活动介绍";
    activityIntroduceLabel.font = [UIFont boldSystemFontOfSize:20];
    [_scrollView addSubview:activityIntroduceLabel];
    [activityIntroduceLabel release];
    
    UILabel *particularLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, HEIGHT * 0.25 + 140, WIDTH - 40, _bigLabelHelght)];
    particularLabel.text = _model.content;
    particularLabel.numberOfLines = 1000;
    [_scrollView addSubview:particularLabel];
    [particularLabel release];
}

// 收藏
- (void)collectButtonItemAction:(UIBarButtonItem *)buttonItem {
//    _block(_model);
    // 查询数据库中的活动收藏表，将结果存入数组
    NSArray *arrActivityModels = [[Fengly_nuo_SQLDataBaseHandle sharedDataBaseHandle] selectTable];
    // 判断数据库返回的数组中是否有该对象，如果有则执行
    if ([arrActivityModels containsObject:_model.title]) {
        UIAlertController *hadCollectAlert = [UIAlertController alertControllerWithTitle:@"收藏提示" message:@"该活动已经被收藏过" preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:hadCollectAlert animated:YES completion:nil];
        [self performSelector:@selector(time) withObject:self afterDelay:0.5];
        
    // 如果不存在，执行
    } else if (![arrActivityModels containsObject:_model.title]) {
        [[Fengly_nuo_SQLDataBaseHandle sharedDataBaseHandle] insertActivityModel:_model];
        UIAlertController *collectAlert = [UIAlertController alertControllerWithTitle:@"收藏提示" message:@"收藏成功" preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:collectAlert animated:YES completion:nil];
        [self performSelector:@selector(time) withObject:self afterDelay:0.5];
    }
}

- (void)time {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)backButtonItemAction:(UIBarButtonItem *)buttonItem {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
